package com.fastCms.business.frame.response;

import com.fastCms.business.frame.constant.ResultCodeConst;
import com.fastCms.business.frame.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description 接口返回工具类
 * @Param
 * @Return
 * @Date 2019/3/7 上午10:53 lsquan
 */
public class ResponseUtil {

    /**
     * @Description 成功
     * @Param [data]
     * @Date 2019/3/7 上午10:54 lsquan
     */
    public static ApiResponse success(Object data) {
        return success(ResultCodeConst.SUCCESS_MSG, data);
    }

    /**
     * @Description 成功，自定义msg
     * @Param [msg, data]
     * @Return com.cloud.rsd.healthPro.common.base.response.ApiResponse
     * @Date 2019/3/7 上午10:54 lsquan
     */
    public static ApiResponse success(String msg, Object data) {
        if (StringUtils.isBlank(msg)) {
            return new Success(ResultCodeConst.SUCCESS_MSG, data);
        }
        return new Success(msg, data);
    }


    /**
     * @Description 失败，自定义失败原因
     * @Param [errMsg]
     * @Date 2019/3/7 上午10:55 lsquan
     */
    public static ApiResponse fail() {
        return new Fail(ResultCodeConst.FAIL_MSG);
    }

    /**
     * @Description 失败，自定义失败原因
     * @Param [errMsg]
     * @Date 2019/3/7 上午10:55 lsquan
     */
    public static ApiResponse fail(String errMsg) {
        if (StringUtils.isBlank(errMsg)) {
            return new Fail(ResultCodeConst.FAIL_MSG);
        }
        return new Fail(errMsg);
    }


    /**
     * @Description 失败，自定义失败编号
     * @Param [errMsg]
     * @Return com.cloud.rsd.healthPro.common.base.response.ApiResponse
     * @Date 2019/3/7 上午10:55 lsquan
     */
    public static ApiResponse fail(Integer code, String errMsg) {
        if (code == null) {
            return new Fail(ResultCodeConst.FAIL_MSG);
        }
        return new Fail(code, errMsg);
    }


    /**
     * @Description 失败，返回失败原因
     * @Param [e]
     * @Date 2019/3/7 上午10:55 lsquan
     */
    public static ApiResponse fail(Exception e) {
        if (e instanceof RuntimeException || e instanceof BusinessException) {
            return fail(e.getMessage());
        } else {
            return new Fail(ResultCodeConst.FAIL_MSG);
        }
    }

    public static ApiResponse tokenFail(String errMsg) {
        if (StringUtils.isBlank(errMsg)) {
            return new Fail(ResultCodeConst.TOKEN_ERROR_CODE, ResultCodeConst.TOKEN_ERROR_DEFAULT_MSG);
        }
        return new Fail(ResultCodeConst.TOKEN_ERROR_CODE, errMsg);
    }

    public static ApiResponse tokenFail() {
        return new Fail(ResultCodeConst.TOKEN_ERROR_CODE, ResultCodeConst.TOKEN_ERROR_DEFAULT_MSG);
    }


}
